//
//  FFTransferInputCell.m
//  LiMaoTransfer
//
//  Created by tt on 2020/9/17.
//

#import "FFTransferInputCell.h"
#import <WuKongBase/WKNumberKeyboardView.h>
@implementation FFTransferInputModel


- (Class)cell {
    return FFTransferInputCell.class;
}

- (NSNumber *)height {
    if(!_height) {
        _height = @(180.0f);
    }
    return _height;
}
@end

@interface FFTransferInputCell ()<UITextFieldDelegate>

@property(nonatomic,strong) UILabel *tipLbl;

@property(nonatomic,strong) UILabel *unitLbl;

@property(nonatomic,strong) UITextField *amountFd;

@property(nonatomic,strong) UILabel *remarkLbl; // 备注

@property(nonatomic,strong) UIButton *transferDescBtn; // 转账说明

@property(nonatomic,strong) WKNumberKeyboardView *keywordView;

@property(nonatomic,strong) FFTransferInputModel *model;

@end

@implementation FFTransferInputCell

+ (CGSize)sizeForModel:(FFTransferInputModel *)model {
    return CGSizeMake(WKScreenWidth, model.height.floatValue);
}

- (void)setupUI {
    [super setupUI];

    
    [self.contentView addSubview:self.tipLbl];
    [self.contentView addSubview:self.unitLbl];
    [self.contentView addSubview:self.amountFd];
    [self.contentView addSubview:self.transferDescBtn];
    [self.contentView addSubview:self.remarkLbl];
}

- (void)refresh:(FFTransferInputModel *)model {
    [super refresh:model];
    self.model = model;
    self.tipLbl.text = LLang(@"转账金额");
    self.unitLbl.text = @"¥";
    self.amountFd.text = model.value;
    if(model.remark && ![model.remark isEqualToString:@""]) {
        self.remarkLbl.text = model.remark;
        [self.transferDescBtn setTitle:LLang(@"修改") forState:UIControlStateNormal];
    }else {
        self.remarkLbl.text = @"";
        [self.transferDescBtn setTitle:LLang(@"添加转账说明") forState:UIControlStateNormal];
    }
    
    [self.amountFd becomeFirstResponder];
    
}

- (UILabel *)tipLbl {
    if(!_tipLbl) {
        _tipLbl = [[UILabel alloc] init];
        [_tipLbl setFont:[[WKApp shared].config appFontOfSize:14.0f]];
        [_tipLbl setTextColor:[WKApp shared].config.defaultTextColor];
    }
    return _tipLbl;
}

- (UILabel *)unitLbl {
    if(!_unitLbl) {
        _unitLbl = [[UILabel alloc] init];
        [_unitLbl setFont:[[WKApp shared].config appFontOfSizeSemibold:30.0f]];
    }
    return _unitLbl;
}

- (UILabel *)remarkLbl {
    if(!_remarkLbl) {
        _remarkLbl = [[UILabel alloc] init];
        [_remarkLbl setFont:[[WKApp shared].config appFontOfSize:14.0f]];
    }
    return _remarkLbl;
}

- (UITextField *)amountFd {
    if(!_amountFd) {
        _amountFd = [[UITextField alloc] init];
        [_amountFd setFont:[[WKApp shared].config appFontOfSizeSemibold:60.0f]];
        self.keywordView = [WKNumberKeyboardView  initWithTextInput:_amountFd];
        self.keywordView.done = LLang(@"转账");
        __weak typeof(self) weakSelf = self;
        [self.keywordView setOnDone:^{
            if(weakSelf.model.onDone) {
                weakSelf.model.onDone();
            }
        }];
        [_amountFd setInputView:self.keywordView];
        [_amountFd addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _amountFd;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    NSInteger length = [self textLength:newString];//转换过的长度
    if (length > 20){
        return NO;
    }

    return YES;
}
-(NSUInteger)textLength: (NSString *) text{

    NSUInteger asciiLength = 0;

      for (NSUInteger i = 0; i < text.length; i++) {
        unichar uc = [text characterAtIndex: i];
            //设置汉字和字母的比例  如何限制4个字符或12个字母 就是1:3  如果限制是6个汉字或12个字符 就是 1:2  一次类推
        asciiLength += isascii(uc) ? 1 : 2;
    }
    NSUInteger unicodeLength = asciiLength;
    return unicodeLength;

  }

-(void) textChange:(UITextField*)textFd {
    if(self.model.onChange) {
        self.model.onChange(textFd.text);
    }
}

- (UIButton *)transferDescBtn {
    if(!_transferDescBtn) {
        _transferDescBtn = [[UIButton alloc] init];
        [_transferDescBtn setTitleColor:[UIColor colorWithRed:94.0f/255.0f green:111.0f/255.0f blue:148.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
        [[_transferDescBtn titleLabel] setFont:[[WKApp shared].config appFontOfSize:14.0f]];
        [_transferDescBtn addTarget:self action:@selector(addRemarkPressed) forControlEvents:UIControlEventTouchUpInside];
    }
    return _transferDescBtn;
}

// 添加备注点击
-(void) addRemarkPressed {
    __weak typeof(self) weakSelf = self;
    UIAlertController *alertController =   [UIAlertController alertControllerWithTitle:LLang(@"转账说明") message:@"" preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = LLang(@"收款方可见，最多10个字");
        textField.delegate = weakSelf;
    }];
    [alertController addAction:[UIAlertAction actionWithTitle:LLang(@"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
               
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:LLang(@"确认") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSString *remark = alertController.textFields[0].text;
        if(weakSelf.model.onRemark) {
            weakSelf.model.onRemark(remark);
        }
    }]];
    [[WKNavigationManager shared].topViewController presentViewController:alertController animated:YES completion:nil];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(20, 20)];
       CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
       maskLayer.frame = self.bounds;
       maskLayer.path = maskPath.CGPath;
       self.layer.mask = maskLayer;
    
    [self.tipLbl sizeToFit];
    self.tipLbl.lim_left = 15.0f;
    self.tipLbl.lim_top = 20.0f;
    
    [self.unitLbl sizeToFit];
    self.unitLbl.lim_left = self.tipLbl.lim_left;
    self.unitLbl.lim_top = self.tipLbl.lim_bottom + 20.0f;
    
    self.amountFd.lim_left = self.unitLbl.lim_right + 5.0f;
    self.amountFd.lim_height = 60.0f;
    self.amountFd.lim_width = self.lim_width - self.amountFd.lim_left * 2.0f;
    self.amountFd.lim_top = self.unitLbl.lim_top + (self.unitLbl.lim_height/2.0f - self.amountFd.lim_height/2.0f) + 5.0f;
    
    self.bottomLineView.lim_left = self.tipLbl.lim_left;
    self.bottomLineView.lim_top = self.amountFd.lim_bottom + 4.0f;
    self.bottomLineView.lim_width = self.lim_width - self.tipLbl.lim_left*2.0f;
    
    [self.transferDescBtn sizeToFit];
    
    [self.remarkLbl sizeToFit];
    self.remarkLbl.lim_left = self.tipLbl.lim_left;
    self.remarkLbl.lim_top = self.bottomLineView.lim_bottom + 20.0f;
    
    self.transferDescBtn.lim_left = self.remarkLbl.lim_right+4.0f;
    self.transferDescBtn.lim_top = self.remarkLbl.lim_top-6.0f;
    
}
- (void)dealloc
{
    WKLogDebug(@"%s",__func__);
}

@end
